package com.example.demo.services;

import com.example.demo.dao.ArticleDao;
import com.example.demo.model.CategoryModel;
import com.example.demo.model.LabelModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import com.example.demo.model.ArticleModel;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class ArticleService {

    @Resource
    ArticleDao articleDao;
    @Resource
    CategoryService categoryService;

    /**
     * 获取所有文章列表（只查单表）
     * @param orderBy
     * @return
     */
    public List<ArticleModel> getArticleAllList(String orderBy){
        return articleDao.getArticleAllList(orderBy);
    }

    /**
     * 获取文章所有列表（包括UV、标签等字段）
     * @param labelId
     * @param categoryId
     * @param orderBy
     * @return
     */
    public List<ArticleModel> getArticleVisitAllList(int labelId, int categoryId, String orderBy){
        return articleDao.getArticleVisitAllList(labelId, categoryId, orderBy);
    }

    /**
     * 获取文章列表（包括UV、标签等字段，带分页）
     * @param pageNum
     * @param pageSize
     * @param labelId
     * @param categoryId
     * @param orderBy
     * @return
     */
    public PageInfo getArticleVisitPageList(
            int pageNum,
            int pageSize,
            int labelId,
            int categoryId,
            String orderBy
    ){
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(getArticleVisitAllList(labelId, categoryId, orderBy));
    }

    public PageInfo getArticleVisitPageList(int pageNum, int labelId, int categoryId){
        return getArticleVisitPageList(pageNum, 10, labelId, categoryId, "id desc");
    }

    /**
     * 获取文章列表（有分页）
     * @param pageNum
     * @param pageSize
     * @param orderBy
     * @return
     */
    public PageInfo getArticlePageList(
            int pageNum,
            int pageSize,
            String orderBy
    ){
        PageHelper.startPage(pageNum, pageSize);
        // 需要获取所有数据的sql
        return new PageInfo(getArticleAllList(orderBy));
    }

    /**
     * 获取文章列表（有分页）
     * @param pageNum
     * @param orderBy
     * @return
     */
    public PageInfo getArticlePageList(int pageNum, String orderBy){
        return getArticlePageList(pageNum, 10, orderBy);
    }

    /**
     * 获取文章列表（有分页）
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo getArticlePageList(int pageNum, int pageSize){
        return getArticlePageList(pageNum, pageSize, "id desc");
    }

    /**
     * 获取文章列表（有分页）
     * @param pageNum
     * @return
     */
    public PageInfo getArticlePageList(int pageNum){
        return getArticlePageList(pageNum, 10, "id desc");
    }

    public PageInfo getArticleListFromDb(
            int pageNum,
            int pageSize
    ) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(getArticleListFromDb(""));
    }

    /**
     * 获取标签所有列表
     * @return
     */
    public List<ArticleModel> getArticleListFromDb(String title){
        List<ArticleModel> articleModelList = articleDao.getArticleList(title);
        HashMap<Long, CategoryModel> categoryList = categoryService.getForIdMapCategoryList();
        // 获取文章类型
        for (ArticleModel articleItem:articleModelList) {
            articleItem.setCategory_title(categoryList.get(articleItem.getCategory_id()).getTitle());
        }
        return articleModelList;
    }

    public ArticleModel getArticleInfo(Long id) {
        return articleDao.getArticleInfoById(id);
    }

    public int add(ArticleModel articleModel) {
        return articleDao.addArticle(articleModel);
    }

    public boolean edit(ArticleModel articleModel) {
        return articleDao.updateArticleById(articleModel);
    }

    public boolean del(Long id) {
        return articleDao.del(id);
    }

}
